package com.example.pufang.controller.front;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.common.CommonResult;
import com.example.pufang.pojo.Report;
import com.example.pufang.pojo.User;
import com.example.pufang.service.ReportService;
import com.example.pufang.service.UserService;
import com.example.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/front/report")
@CrossOrigin
@Api(tags = "h5举报接口")
public class ReportFrontController {

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private ReportService reportService;

    @Operation(summary = "提交举报")
    @PostMapping("submit")
    public CommonResult<Boolean> submitReport(@RequestBody Report report, HttpServletRequest request) {

        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        if (ObjectUtils.isNull(user)) {
            return CommonResult.failed("个人信息获取失败");
        }

        if (reportService.submitReport(report, user)) {
            return CommonResult.success("举报成功");
        } else {
            return CommonResult.failed("举报失败");
        }

    }

}
